/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} phone
 * @returns {Boolean}
 */
export function isPhone(val) {
  // const reg = /^1[3456789]\d{9}$/;
  const reg = new RegExp("^1[3456789]\\d{9}$");
  return reg.test(val);
}


/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

// 校验手机号码
export var validPhone = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入手机号码'))
  } else if (!isPhone(value)) {
    callback(new Error('请输入正确的11位手机号码'))
  } else {
    callback()
  }
}



// 不能为负数
export var validNumber = (rule, value, callback) => {
  if (value<0) {
    callback(new Error('不能为负数'))
  } else {
    callback()
  }
}
// 密码
export var validPassword = (rule, value, callback) => {
  if (value && value != '') {
    let pwdCount = 0
    let pwdRegex1 = new RegExp('(?=.*[0-9]).{8,30}')//纯数字
    let pwdRegex2 = new RegExp('(?=.*[a-z]).{8,30}')//小写字母
    let pwdRegex3 = new RegExp('(?=.*[A-Z]).{8,30}')//大写字母
    let pwdRegex4 = new RegExp('(?=.*[^a-zA-Z0-9]).{8,30}')//特殊字符
    if (pwdRegex1.test(value)) {
      pwdCount = pwdCount + 1
    }
    if (pwdRegex2.test(value)) {
      pwdCount = pwdCount + 1
    }
    if (pwdRegex3.test(value)) {
      pwdCount = pwdCount + 1
    }
    if (pwdRegex4.test(value)) {
      pwdCount = pwdCount + 1
    }
    if (value.length > 9) {
      pwdCount = pwdCount + 1
    }
    if (pwdCount < 5) {
      callback(new Error('密码至少包含大写字母、小写字母、数字、特殊符号中的4种，且长度至少为10位！'))
    }
    callback()
  } else {
    callback()
  }
}

/**
 * 
 * @param {文件大小} fileSize 
 * @param {限制大小} overSize 
 */
export function isOverSize(fileSize, overSize) {
  var size = Math.round(fileSize / 1024 * 100) / 100;
  if (size > overSize * 1024) {
    return true;
  } else {
    return false;
  }
}



/**
 * 时间转为秒
 * @param time 时间(00:00:00)
 * @returns {string} 时间戳（单位：秒）
 */
export function time_to_sec(time) {
  let s = ''
  let hour = time.split(':')[0]
  let min = time.split(':')[1]
  let sec = time.split(':')[2]
  s = Number(hour * 3600) + Number(min * 60) + Number(sec)
  return s
}


/**
 * 时间秒数格式化
 * @param s 时间戳（单位：秒）
 * @returns {*} 格式化后的时分秒
 */
export function sec_to_time(s) {
  let t
  if (s > -1) {
    let hour = Math.floor(s / 3600)
    let min = Math.floor(s / 60) % 60
    let sec = s % 60
    if (hour < 10) {
      t = '0' + hour + ':'
    } else {
      t = hour + ':'
    }
    if (min < 10) {
      t += '0'
    }
    t += min + ':'
    if (sec < 10) {
      t += '0'
    }
    t += sec
    // .toFixed(2)
  }
  return t
}
/**
 * 
 * @param {*} type start：开始时间，end：结束时间
 * @param {*} dates 日期方向，默认0：本周，-1：上周，1：下周
 * @returns 
 */
function getMonday(type, dates = 0) {
  var now = new Date();
  var nowTime = now.getTime();
  var day = now.getDay();
  var longTime = 24 * 60 * 60 * 1000;
  var n = longTime * 7 * (dates || 0);
  if (type == "start") {
    var dd = nowTime - (day - 1) * longTime + n;
  };
  if (type == "end") {
    var dd = nowTime + (7 - day) * longTime + n;
  };
  dd = new Date(dd);
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1;
  var d = dd.getDate();
  m = m < 10 ? "0" + m : m;
  d = d < 10 ? "0" + d : d;
  var day = y + "-" + m + "-" + d;
  return day;
};
/**
 * 
 * @param {*} type start：开始时间，end：结束时间
 * @param {*} dates 日期方向，默认0：今年，-1：去年，1：明年
 * @returns 
 */
function getYear(type, dates) {
  var dd = new Date();
  var n = dates || 0;
  var year = dd.getFullYear() + Number(n);
  if (type == "start") {
    var day = year + "-01-01";
  };
  if (type == "end") {
    var day = year + "-12-31";
  };
  if (!type) {
    var day = year + "-01-01/" + year + "-12-31";
  };
  return day;
}


// 返回时间零点与23：59：59
export function time_date(val) {
  let t_date = {
    start: '',
    end: ''
  }
  // 当天
  if (val == 1) {
    t_date.start = new Date(new Date(new Date().toLocaleDateString()).getTime())
    t_date.end = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
  }
  // 昨天
  if (val == -1) {
    const today = new Date();
    today.setHours(0);
    today.setMinutes(0);
    today.setSeconds(0);
    today.setMilliseconds(0);
    t_date.end = new Date(today - 1000);
    t_date.start = new Date(today - 3600 * 1000 * 24);
  }

  // 前天
  if (val == -2) {
    const today = new Date();
    today.setHours(0);
    today.setMinutes(0);
    today.setSeconds(0);
    today.setMilliseconds(0);
    t_date.end = new Date(today - (3600 * 1000 * 24) - 1000);
    t_date.start = new Date(today - 2 * 3600 * 1000 * 24);
  }

  if (val == 7) {
    t_date.start = getMonday('start', 0)
    let end = getMonday('end', 0) + ' 23:59:59'
    t_date.end = end
  }
  if (val == -7) {
    t_date.start = getMonday('start', -1)
    t_date.end = getMonday('end', -1) + ' 23:59:59'
  }
  // 本月
  if (val == 30) {
    let today = new Date();
    t_date.start = new Date(today.getFullYear(), today.getMonth(), 1); //本月第一天
    let end = new Date(today.getFullYear(), today.getMonth() + 1, 0)
    end.setHours(23)
    end.setMinutes(59)
    end.setSeconds(59)
    t_date.end = end;//本月最后一天
  }
  // 上月
  if (val == -30) {
    let today = new Date();
    t_date.start = new Date(today.getFullYear(), today.getMonth() - 1, 1); //上月第一天
    let end = new Date(today.getFullYear(), today.getMonth(), 0)
    end.setHours(23)
    end.setMinutes(59)
    end.setSeconds(59)
    t_date.end = end;//上月最后一天
  }

  if (val == 365) {
    t_date.start = getYear('start', 0)
    let end = getYear('end', 0) + ' 23:59:59'
    t_date.end = end
  }
  if (val == -365) {
    t_date.start = getYear('start', -1)
    t_date.end = getYear('end', -1) + ' 23:59:59'
  }

  return t_date
}



export function integerZeroFn(value) {
  if(value==='') return ''
  let reg = /^(0|\+?[1-9][0-9]*)$/;
  let strArray = value.split("");
  let newStrs = "";
  for (let i = 0; i < strArray.length; i++) {
    if (reg.test(strArray[i])) {
      console.log(strArray[i]);
      newStrs += strArray[i];
    } else if (i > 0 && strArray[i] === "0") {
      newStrs += strArray[i];
    }
  }
  if (!(newStrs - 0 < 0)) {
    return newStrs - 0;
  } else {
    return "";
  }
}
